<?php
/**
 * @Notes:【】
 * @User: zwc
 * @Date: 2022-05-01 17:20
 */

namespace App\Dao\Chat;

use App\Model\Chat\ChatFd;
use Yj\Daos\BaseDao;
use Hyperf\Database\Model\Builder;
use Yj\Daos\Verify;

/**
 * @var \App\Model\Chat\ChatSession
 */
class ChatSessionDao extends BaseDao
{
    public function DaoWhere(array $params)
    {
        return $this->getDaoQuery($params, function (Verify $verify) {
            $verify('user_id', function (Builder $query, $user_id) {
                $query->where('user_id', $user_id);
            });
            $verify('title', function (Builder $query, $title) {
                $query->where('title', $title);
            });
        });
    }

    public function add(int $sender_id, int $receiver_id, int $last_message_id)
    {
        $this->updateOrCreate([
            'user_id' => $sender_id,
            'receiver_id' => $receiver_id
        ], [
            'last_message_id' => $last_message_id
        ]);
        $this->updateOrCreate([
            'user_id' => $receiver_id,
            'receiver_id' => $sender_id
        ], [
            'last_message_id' => $last_message_id
        ]);
    }

}
